<?php

namespace App\Listeners;
use App\Events\CronProcessingEvent;
use App\Models\OrdersModel;
use App\Services\CronService;
use App\Services\OrderService;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;
use Psr\Log\LoggerInterface;

class CronProcessingListener
{


    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {

    }


    /**
     *
     * todo 处理订单状态
     * @param CronProcessingEvent $event
     */
    public function handle(CronProcessingEvent $event)
    {

        $event_name = trim($event->event_name);
        $id = intval($event->relate_id);
        $server = new OrderService();
//        app(LoggerInterface::class)->info('执行任务：'.$event_name);

        switch ($event_name)
        {
            case 'OrderComplete':
                //订单完成：订单7天（用户未确认）
                $do_result = $server->CronOrderComplete($id);
                break;
            case 'OrderNoPayCancel':
                //订单未支付自动取消
                $do_result = $server->CronOrderNoPayCancel($id);
                break;
            case 'OrderNoSureRefund':
                //订单自动关闭
                $do_result = $server->CronOrderNoSureRefund($id);
                break;
            case 'OrderRefund':
                //订单自动退款
                $do_result = $server->autoOrderRefund($id);
                break;
            default:
                $do_result = '++++++++++++++任务类型未命中：';
        }

        if($do_result !== true){
            Log::warning("{$event_name} {$id} : {$do_result}");
        }

    }

}
